/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.entidades;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.content.Context;
import android.content.ContextWrapper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Date;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;

/**
 *
 * @author efraingutierrez
 */
public class Update 
{
    private static final String userFile = "update.txt";
     
   
    public static boolean existeArchivo()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            myReader.close();
            return true;
        } 
        catch (Exception e) {
            return false;
        }
    }
    
    public static Date getLastUpdate()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) 
            {
                aBuffer += aDataRow;
            }
            myReader.close();
            return new Date(aBuffer);
        } 
        catch (Exception e) 
        {
            updateTime();
            return getLastUpdate();
        }
    }
    
    public static void updateTime() 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter);
            out.write(""+new Date());
            out.close();
        } 
        catch (Exception e) {
            Mensaje.Mostrar(e.toString());
        }
    }
    
    public static Boolean isTime()
    {
        Date now = new Date();
        Date lastUpdate = getLastUpdate();
        long difMillis = now.getTime() - lastUpdate.getTime();
        int minutos = Cronometro.getMinutos();
        long horasMillis = MILLISECONDS.convert(minutos, MINUTES);
        return (difMillis>=horasMillis);
    }
  
      
}
